package com.holly.common.utils;

import android.text.TextUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 生成数字
 * 2016/10/19 <br/>
 *
 * @author Joey Huang QQ:273179370）
 */

public class ToNumUtils {

    public static int toInt(String intStr) {
        if (TextUtils.isEmpty(intStr)) {
            return 0;
        }
        try {
            int i = Integer.parseInt(intStr);
            return i;
        } catch (Exception e) {
            return 0;
        }
    }

    public static double getDoubleFromString(String s) {
        if (TextUtils.isEmpty(s)) {
            return 0;
        }
        try {
            return Double.parseDouble(s);
        } catch (NumberFormatException e) {
            return 0;
        }
    }

    /**
     * 可以对任意Double 进行百分比格式化
     * 比如 value = 1.23456
     * 如果 percentPointCount <= 0  结果为 1%
     * 如果 percentPointCount = 2   结果为 123.45%
     * 如果 percentPointCount = 4   结果为 123.4560%
     * @param value             数值
     * @param percentPointCount 百分比小数位数，比如 20.35% 表示位数为2
     * @return 带百分号的String
     */
    public static String toPercent(Double value, int percentPointCount) {
        if (percentPointCount < 0) {
            percentPointCount = 0;
        }
        if (value == null || value == Double.NaN) {
            String valueStr = "0";
            if (percentPointCount > 0) {
                valueStr += ".";
                for (int i = 0; i < percentPointCount; i++) {
                    valueStr += "0";
                }
            }
            valueStr += "%";
            return valueStr;
        }
        String valueStr = String.valueOf(value * 100);
        if (valueStr.contains(".")) {
            int indexOf = valueStr.indexOf(".");
            int length = valueStr.length();
            //当前小数位数
            int currPointCount = length - indexOf - 1;
            if (currPointCount > percentPointCount) {
                valueStr = valueStr.substring(0, length - (currPointCount - percentPointCount));
            } else if (currPointCount < percentPointCount) {
                for (int j = 0; j < percentPointCount - currPointCount; j++) {
                    valueStr += "0";
                }
            }
        } else {
            //表示没有小数点
            valueStr += ".";
            for (int i = 0; i < percentPointCount; i++) {
                valueStr += "0";
            }
        }
        valueStr += "%";
        return valueStr;
    }

    public static String toPercent(String value, int percentPointCount) {
        Double optDouble = optDouble(value);
        return toPercent(optDouble, percentPointCount);
    }

    /**
     * 如果不属于 Double型，则返回null
     *
     * @param value 数值 可以任意字段
     * @return
     */
    public static Double optDouble(String value) {
        if (value == null || value.isEmpty()) {
            return null;
        }
        try {
            Double v = Double.parseDouble(value);
            if (v != Double.NaN) {
                return v;
            } else {
                return null;
            }
        } catch (Exception e) {
            return null;
        }
    }

    //四舍五入
    public static Double optDouble(String value,int pointCount){
        Double optDouble = optDouble(value);
        if (optDouble == null) {
            return null;
        }
        BigDecimal b   =   new   BigDecimal(optDouble);
        return b.setScale(pointCount,   RoundingMode.HALF_UP).doubleValue();
    }
}
